home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sharutil.2 / sharutil / sharutils-4.2 / lib / whoami.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-16  |  2.8 KB  |  119 lines

  1. /* Find out who I am & where I am.
  2.    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software Foundation,
  16.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. #include "system.h"
  20.  
  21. /* Size of buffers holding strings.  */
  22. #define STRING_BUFFER_SIZE 64
  23.  
  24. #include <pwd.h>
  25.  
  26. /* On Ultrix 2.x, <utsname.h> uses SYS_NMLEN, which is only defined in
  27.    <limits.h>.  Sigh!  */
  28.  
  29. #ifdef HAVE_UNAME
  30. # ifdef HAVE_LIMITS_H
  31. #  include <limits.h>
  32. # endif
  33. # include <sys/utsname.h>
  34. # include <time.h>
  35. #else
  36. # include <sys/time.h>
  37. #endif
  38.  
  39. #ifdef HAVE_UNISTD_H
  40. # include <unistd.h>
  41. #endif
  42.  
  43. #ifndef _POSIX_VERSION
  44. struct passwd *getpwuid ();
  45. #endif
  46.  
  47. /*----------------.
  48. | Get user name.  |
  49. `----------------*/
  50.  
  51. static const char *
  52. get_username ()
  53. {
  54.   struct passwd *passwd;
  55.  
  56.   passwd = getpwuid (getuid ());
  57.   endpwent ();
  58.   if (passwd == NULL)
  59.     return "???";
  60.   return passwd->pw_name;
  61. }
  62.  
  63. /*------------------------------------------------------.
  64. | Do uname, gethostname, or read file (/etc/systemid).  |
  65. `------------------------------------------------------*/
  66.  
  67. static const char *
  68. get_hostname ()
  69. {
  70. #if HAVE_UNAME
  71.  
  72.   static struct utsname hostname_buffer;
  73.  
  74.   uname (&hostname_buffer);
  75.   return hostname_buffer.nodename;
  76.  
  77. #else
  78. # if HAVE_ETC_SYSTEMID
  79.  
  80.   FILE *fpsid = fopen ("/etc/systemid", "r");
  81.   static char buffer[STRING_BUFFER_SIZE];
  82.  
  83.   if (!fpsid)
  84.     return "???";
  85.   fgets (buffer, sizeof (buffer), fpsid);
  86.   fclose (fpsid);
  87.   buffer[strlen (buffer) - 1] = 0;
  88.   return buffer;
  89.  
  90. # else
  91.  
  92.   static char hostname_buffer[STRING_BUFFER_SIZE];
  93.  
  94.   gethostname (hostname_buffer, sizeof (hostname_buffer));
  95.   return hostname_buffer;
  96.  
  97. # endif
  98. #endif
  99. }
  100.  
  101. /*------------------------------------------------------------------.
  102. | Fill BUFFER with a string representing the username and hostname  |
  103. | separated by an `@' sign, and return a pointer to the constructed |
  104. | string.  If BUFFER is NULL, use an internal buffer instead.        |
  105. `------------------------------------------------------------------*/
  106.  
  107. char *
  108. get_submitter (buffer)
  109.      char *buffer;
  110. {
  111.   static char static_buffer[STRING_BUFFER_SIZE];
  112.  
  113.   if (!buffer)
  114.     buffer = static_buffer;
  115.   strcpy (buffer, get_username ());
  116.   strcat (buffer, "@");
  117.   return strcat (buffer, get_hostname ());
  118. }
  119.